package com.activity.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.activity.bean.ClassSpan;
import com.activity.fragment.NewClassActivity;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.adapter.BaseAdapter;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.google.gson.Gson;

import androidx.annotation.NonNull;
import butterknife.BindView;

public class ClassSetAdapter extends BaseAdapter<ClassSpan> {


    private TextWatcher textWatcher;
    private View mview;
    private String mtext ="";

    public ClassSetAdapter(Context context) {
        super(context);
    }

    public  ClassListener classListener ;

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.header_class;
    }

    @Override
    public BaseViewHolder<ClassSpan> getViewHolder(int viewType, View view) {

        if(viewType==TYPE_CHILD)
        return new ClassSpanViewHolder(view);


        return null;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_class_cell;
    }


    @Override
    public void onBindHeaderViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {
        holder.itemView.findViewById(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ActivityUtil.startActivity(mContext, NewClassActivity.class);
            }
        });
        EditText editText =  holder.itemView.findViewById(R.id.tv_search_bar);

        editText.setText(mtext);
        editText.setSelection(mtext.length());
        if(editText.getTag()==null){
            textWatcher =  new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {


                    mtext =editText.getText().toString();
                    if(classListener!=null){
                        classListener.textChange(editText.getText().toString());
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            };

            editText.addTextChangedListener(textWatcher);
            editText.setTag("1");
        }




    }

    @Override
    public void onBindChildViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition, int childPosition) {
        super.onBindChildViewHolder(holder, groupPosition, childPosition);
        ClassSpanViewHolder  classHolder = (ClassSpanViewHolder) holder;
        classHolder.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                v.setSelected(!v.isSelected());
                getData().get(childPosition).selected = !getData().get(childPosition).selected;
                Intent intent = new Intent();
                Gson gson =new Gson();
                intent.putExtra("data",gson.toJson(getData().get(childPosition)));
                ((Activity)mContext).setResult(100,intent);
                ((Activity)mContext).finish();
            }
        });
        classHolder.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle param =new Bundle();
                Gson gson = new Gson();
                param.putString("param",gson.toJson(getData().get(childPosition)));

                ActivityUtil.startActivity(mContext, NewClassActivity.class,param);

            }
        });

    }

    public class ClassSpanViewHolder extends BaseViewHolder<ClassSpan> {
        @BindView(R.id.iv_check)
        ImageView ivCheck;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time_span)
        TextView tvTimeSpan;
        @BindView(R.id.tv_hour)
        TextView tvHour;
        @BindView(R.id.iv_edit)
        ImageView ivEdit;
        public ClassSpanViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        @Override
        public void setData(ClassSpan data) {
            ivCheck.setSelected(data.selected);
            tvName.setText(data.getAttendacetimespanname());
            tvTimeSpan.setText(data.getAttendacetimespan_begintime()+"-"+data.getAttendacetimespan_endtime());
            tvHour.setText(data.getHours());

        }

    }

    public  interface  ClassListener{
        public  void textChange(String text);
    }


}
